using UnityEngine;

//塔
public class LaserTower : Tower
{

    //跟踪目标
    TargetPoint target;

    [SerializeField]
    Transform turret = default, laserBeam = default;

    //激光缩放
    Vector3 laserBeamScale;

    //DPS
    [SerializeField, Range(1f, 100f)]
    float damagePerSecond = 10f;

    public override TowerType TowerType => TowerType.laser;


    void Awake()
    {
        laserBeamScale = laserBeam.localScale;
    }

    void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.yellow;
        Vector3 position = transform.localPosition;
        position.y += 0.01f;
        Gizmos.DrawWireSphere(position, targetingRange);
        if(target != null)
        {
            Gizmos.DrawLine(position, target.Position);
        }
    }

    public override void GameUpdate()
    {
        //是否有目标
        if(TrackTarget(ref target) || AcquireTarget(out target))
            Shoot();
        else{
            
            laserBeam.localScale = Vector3.zero;
        }
    }

    void Shoot()
    {
        Vector3 point = target.Position;
        //旋转变换，使向前矢量指向 target 的当前位置。
        turret.LookAt(point);
        //设置激光旋转
        laserBeam.localRotation = turret.localRotation;

        
        float d = Vector3.Distance(turret.position, point);
        laserBeamScale.z = d;
        laserBeam.localScale = laserBeamScale;
        laserBeam.localPosition = turret.localPosition + 0.5f * d * laserBeam.forward;

        //乘以时间增量即为每秒的DPS
        target.Enemy.ApplyDamage(damagePerSecond * Time.deltaTime);
    }

}